<!-- 小区编辑表单 -->
<template>
  <q-card style="width: 650px">
    <!-- 标题 -->
    <q-toolbar>
      <q-toolbar-title>{{ title }}</q-toolbar-title> <q-btn v-close-popup icon="close" round flat />
    </q-toolbar>
    <q-separator />

    <!-- 内容 -->
    <q-card-section>
      <q-form ref="formRef" class="row q-col-gutter-x-md">
        <q-select v-model="dto.communityId" label="社区名称" :rules="[required]" class="col-12" :options="ctOps" emit-value map-options />
        <q-input v-model="dto.areaName" label="小区名称" :rules="[required]" class="col-12 col-sm-12" />
        <q-input v-model="dto.areaAddr" label="小区地址" :rules="[required]" class="col-12 col-sm-12" />
        <q-input v-model.number="dto.buildArea" label="建筑面积" class="col-12 col-sm-4" :rules="[numeric, gt(0)]">
          <template #append>万m²</template>
        </q-input>
        <q-input v-model.number="dto.totalArea" label="总面积" class="col-12 col-sm-4" :rules="[numeric, gt(0)]">
          <template #append>万m²</template>
        </q-input>
        <q-input v-model.number="dto.plantArea" label="绿化面积" class="col-12 col-sm-4" :rules="[numeric, gt(0)]">
          <template #append>万m²</template>
        </q-input>
        <q-input v-model="dto.remark" label="小区简介" class="col-12 q-mt-md" type="textarea" rows="5" />
      </q-form>
    </q-card-section>
    <q-separator />

    <!-- 操作 -->
    <q-card-actions v-if="!readonly">
      <q-checkbox v-model="closeAfterSave" label="保存后关闭" />
      <q-space />
      <q-btn v-close-popup>取消</q-btn>
      <q-btn color="primary" icon="save" :loading="loading" @click="handleSubmit">保存</q-btn>
    </q-card-actions>
  </q-card>
</template>

<script lang="ts" setup>
  import { gt, numeric, required } from 'src/utils/validator';
  import { queryOptions, useCrudForm } from 'src/composables';

  import crudApi, { AreaDto } from 'src/api/community/area';
  import { onMounted, ref } from 'vue';

  // 属性和事件定义
  const props = defineProps({
    id: { type: [String, Number], default: undefined },
    title: { type: String, default: '小区表单' },
    readonly: { type: Boolean, default: false },
  });
  const emit = defineEmits(['close', 'after-save']);

  // 表单数据
  const initArea = { disabled: false } as AreaDto;
  const { formRef, dto, closeAfterSave, loading, handleSubmit } = useCrudForm<AreaDto>(crudApi, initArea, props, emit);
  const ctOps = ref<SelectOption[]>([]);
  onMounted(() => {
    queryOptions('community').then((data) => {
      ctOps.value = data;
      if (data.length == 1) {
        dto.value.communityId = data[0].value as number;
      }
    });
  });
</script>

<style lang="scss" scoped></style>
